<?php 
class CAdvertisementController extends CController{

	protected $m_arrobjUsers;
	protected $m_objUser;
	protected $m_objAdvertisement;
	protected $m_arrobjAdvertisementTypes;
	protected $m_arrobjPages;
	
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'view_advertisements':
				$this->handleViewAdvertisements();
				break;
				
			case 'add':
			case 'edit':
						
				$this->handleAddEditAdvertisement();
				break;
							
			case 'insert_update_advertisement':
					$this->handleInsertUpdateAdvertisement();
				break;
			case 'update_advertisement_action':
				$this->handleUpdateAdvertisementAction();
				break;
			
			case 'view_advertisement_in_detail':
					$this->handleAdvertisementInDetail();
			break;
					
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewAdvertisements() {
		$this->m_arrobjAdvertisements = CAdvertisements::fetchAdvertisements( $this->m_objDatabase );
		$this->m_arrobjAdvertisementCategories = CAdvertisementCategories::fetchAdvertisementCategories( $this->m_objDatabase );
		$this->m_arrobjAdvertisementTypes = CAdvertisementTypes::fetchAdvertisementTypes( $this->m_objDatabase );
		
		$this->displayAdvertisements();
	}
	
	function handleAddEditAdvertisement() {
		
		$this->m_objAdvertisement = new CAdvertisement();
		
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objAdvertisement = CAdvertisements::fetchAdvertisementById( $_REQUEST['id'], $this->m_objDatabase );
		}
		
		
		$this->m_arrobjAdvertisementTypes = CAdvertisementTypes::fetchAdvertisementTypes( $this->m_objDatabase );
		$this->m_arrobjAdvertisementCategories = CAdvertisementCategories::fetchAdvertisementCategories( $this->m_objDatabase );
		
		$this->displayAddEditAdvertisement();
	}
	
	
	function handleInsertUpdateAdvertisement() {
		
		include( LIABRARIES_PATH . "Common/common.lib.php");
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'advertisement' ) );
		
				$this->m_objAdvertisement = CPages::setValues( 'CAdvertisement', $arrmixFieldsValues );

				$width_array = array("191");
				$height_array = array("112");
				$strPath = MOUNT_PATH.'Advertisement/';
				$path_array = array($strPath);
				$original_1=resize_multiple_images($_FILES['file'],$width_array,$path_array,$original_img_path,$height_array);
				$this->m_objAdvertisement->setImage($original_1);
							
				/* if( false == $this->m_objFaq->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				} */
				
				if( false == is_null( $this->m_objAdvertisement->getId() ) && '' != trim( $this->m_objAdvertisement->getId() ) ) {
				
					if( false == $this->m_objAdvertisement->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update Advertisement.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Advertisement update successfully. ' );
					}
				} else {
					
					if( false == $this->m_objAdvertisement->insert( $_SESSION['udeal.in']['login_user_id'], $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create Advertisement.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Advertisement created successfully. ' );
					}
				}
				redirect( '/admin/advertisement' );
		}
 		redirect( '/admin/advertisement/add' );
	}

	function handleUpdateAdvertisementAction() {
	
		switch( true ) {
			default:
	
				$strAction = $this->m_objData->getPostData( array( 'advertisement', 'action' ) );
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
	
				$arrintAdvertisementIds = $this->m_objData->getPostData( array( 'advertisement', 'id' ) );
				if( false == valArr( $arrintAdvertisementIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
	
				$this->m_objAdvertisement = new CAdvertisement();
				if( false == valObj( $this->m_objAdvertisement, 'CAdvertisement' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
	
				if( 'delete' == $strAction ) {
					if( true == $this->m_objAdvertisement->deleteAdvertisementsByIds( $arrintAdvertisementIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objAdvertisement->updateAdvertisementsActionByIds( $strAction, $arrintAdvertisementIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
					
		}
		redirect( '/admin/advertisement' );
	}
	
	function handleAdvertisementInDetail() {
		$intAdvertisementId = $_REQUEST['id'];
		$this->m_objAdvertisement = CAdvertisements::fetchAdvertisementById( $intAdvertisementId, $this->m_objDatabase );
		$this->m_arrobjAdvertisementTypes = CAdvertisementTypes::fetchAdvertisementTypes( $this->m_objDatabase );
		$this->m_arrobjAdvertisementCategories = CAdvertisementCategories::fetchAdvertisementCategories( $this->m_objDatabase );
		
		
		$this->displayAdvertisementInDetail();
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayAddEditAdvertisement() {
		$this->display();

		$this->m_objSmarty->assign_by_ref( 'advertisement', $this->m_objAdvertisement );
		$this->m_objSmarty->assign_by_ref( 'advertisement_types', $this->m_arrobjAdvertisementTypes );
		$this->m_objSmarty->assign_by_ref( 'advertisement_categories', $this->m_arrobjAdvertisementCategories );
		
		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement/view_add_edit_advertisement.tpl' );
	}
	
	function displayAdvertisements() {
		$this->display();

		$this->m_objSmarty->assign( 'advertisements', $this->m_arrobjAdvertisements );
		$this->m_objSmarty->assign_by_ref( 'advertisement_types', $this->m_arrobjAdvertisementTypes );
		$this->m_objSmarty->assign_by_ref( 'advertisement_categories', $this->m_arrobjAdvertisementCategories );
		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement/view_advertisements.tpl');

	}
	
	function displayAdvertisementInDetail() {
		$this->display();
	
		$this->m_objSmarty->assign( 'advertisement', $this->m_objAdvertisement );
		$this->m_objSmarty->assign_by_ref( 'advertisement_types', $this->m_arrobjAdvertisementTypes );
		$this->m_objSmarty->assign_by_ref( 'advertisement_categories', $this->m_arrobjAdvertisementCategories );
		
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement/view_advertisement_in_detail.tpl');
	
	}
	
	function display() {
	
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'advertisement' );
	}

}
?>